namespace NBody.Analysis
{
    using System;
    
    public class Bar
    {
        public static double CalculateStrength(NBodySystem s, double rmin, double rmax)
        {
            int n = 0;
            double re = 0.0;
            double im = 0.0;
            
            Console.Error.WriteLine("Calculating bar strength between radius = {0} and {1}", rmin, rmax);
            foreach (Particle p in s)
            {
                double radius = p.Radius;
                if (radius < rmax && radius > rmin)
                {
                    double phi = p.Phi;
                    re += Math.Cos(2.0 * phi);
                    im += Math.Sin(2.0 * phi);
                    n++;
                }
            }
            
            return Math.Sqrt(re * re + im * im) / (double)n;
        }
    }
}
